package com.example.administrator.balcklist.util;

import com.example.administrator.balcklist.qcos.QServiceCfg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/15 0015.
 */

public class BlackNumUtil {
    public static List<String> getNums() {
        List<String> nums = new ArrayList<>();
        try {
            File file = new File(QServiceCfg.LOCAL_PATH);
            InputStream is = new FileInputStream(file);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                nums.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return nums;
    }

    public static boolean addBlackNum(String num){
        File file = new File(QServiceCfg.LOCAL_PATH);
        RandomAccessFile raf = null;
        try {
            //如果为追加则在原来的基础上继续写文件
            raf = new RandomAccessFile(file, "rw");
            raf.seek(file.length());
            raf.write("\n".getBytes());
            raf.write(num.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

}
